﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using br.com.SistemaChamados.Enum;
using System.Data;
using br.com.SistemaChamados.Model;

/// <summary>
/// Summary description for callStatusDAO
/// </summary>
/// 
namespace br.com.SistemaChamados.DAO
{
    public class CallDAO
    {
        private static string SQL_INSERT_CALL = " INSERT INTO TBL_CALL (TBL_CALLID, TBL_CALLPROBLEMDESCRIPTION, TBL_USEROPEN, TBL_CALLSTATUSID, TBL_CALLSUBCATEGORYID, TBL_SECTORID) VALUES (@callid, @callprobdescription, @useropen, @callstatusid, @callsubcategoryid, @sectorid) ";
        private static string SQL_GET_CALLID = " SELECT * FROM FUN_GETCALLID() ";

        public int? GetCallId()
        {
            IDbConnection conn = null;
            IDbCommand command = null;
            IDataReader reader = null;
            int? callId = null;
            try
            {
                conn = Mapped.Connection();
                command = Mapped.Command(SQL_GET_CALLID, conn);
                reader = command.ExecuteReader(CommandBehavior.CloseConnection);
                while (reader.Read())
                {
                    callId = Convert.ToInt32(reader["FUN_GETCALLID"]);
                }
                return callId;
            }
            catch (Exception ex)
            {
                return callId;
                throw ex;
            }
            finally
            {
                if (conn != null) conn.Close();
            }

        }
        
        public EnumGenericState InsertCall(Call call)
        {
            EnumGenericState resultCall = EnumGenericState.GENERIC_STATE_UNKNOW;
            IDbConnection conn = null;
            IDbCommand command = null;
            try
            {
                conn = Mapped.Connection();
                command = Mapped.Command(SQL_INSERT_CALL, conn);
                command.Parameters.Add(Mapped.Parameter("@callid", call.Id));
                command.Parameters.Add(Mapped.Parameter("@callprobdescription", call.ProblemDescription));
                command.Parameters.Add(Mapped.Parameter("@useropen", call.UserOpen.Id));
                command.Parameters.Add(Mapped.Parameter("@callstatusid", call.CallStatus.Id));
                command.Parameters.Add(Mapped.Parameter("@callsubcategoryid", call.CallSubCategory.Id));
                command.Parameters.Add(Mapped.Parameter("@sectorid", call.Sector.Id));
                command.ExecuteNonQuery();
                resultCall = EnumGenericState.GENERIC_STATE_SUCCESS;
            }
            catch (Exception ex)
            {
                resultCall = EnumGenericState.GENERIC_STATE_FAILED;
                throw ex;
            }
            finally
            {
                if (conn != null) conn.Close();
            }

            return resultCall;

        }
        public CallDAO()
        {
            //
            // TODO: Add constructor logic here
            //
        }
    }
}